package cn.wolfcode.luowowo.packagetour.service.impl;

import cn.wolfcode.luowowo.packagetour.repository.IPackageTourContentTemplateRepository;
import cn.wolfcode.luowowo.packagetour.service.IPackageTourContentService;
import cn.wolfcode.luowowo.packagetour.template.PackageTourContentTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import java.util.Iterator;

@Service
public class PackageTourContentServiceImpl implements IPackageTourContentService {
    @Autowired
    private IPackageTourContentTemplateRepository dao;
    @Autowired
    private ElasticsearchTemplate template;
    @Autowired
    private TransportClient client;

    //保存数据
    public void save(PackageTourContentTemplate packageTourContentTemplate) {
        dao.save(packageTourContentTemplate);
    }

    //搜索指定id的跟团游信息
    public PackageTourContentTemplate searchById(Long id) {
        Iterable<PackageTourContentTemplate> iterable = dao.search(QueryBuilders.matchQuery("id", id));
        Iterator<PackageTourContentTemplate> iterator = iterable.iterator();
        if (iterator.hasNext()){
            return iterator.next();
        }
        return new PackageTourContentTemplate();
    }

    //创建索引
    public void createIndex() {
        template.createIndex(PackageTourContentTemplate.class);
        template.getMapping(PackageTourContentTemplate.class);
    }


}
